﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.UI.HtmlControls;
using System.Data;
public partial class AdminPortal_Controls_Menu_MenuList : System.Web.UI.UserControl
{
    string LangID = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
    TblMenuManager _TblMenuManager = new TblMenuManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            TblMenuEntity _TblMenuEntity = new TblMenuEntity();
            _TblMenuEntity.SortExpression = TblMenuFields.ThuTu.Name;
            _TblMenuEntity.SortDirection = "ASC";
            _TblMenuEntity.LangId = LangID;

            LoadDataToGrid(_TblMenuEntity);

            LoadDanhMucCha();
        }
    }
    private void LoadDanhMucCha()
    {
        DataTable _dtCha = _TblMenuManager.SelectAllRDT();
        if (_dtCha != null)
        {
            HNG.LIB.BIND_SOURCE.Bind_DropDownListV3(ddlDanhMucCha, _dtCha, TblMenuFields.TieuDe.Name, TblMenuFields.Id.Name, TblMenuFields.ParentId.Name);
        }
    }

    protected void ddlDanhMucCha_SelectedIndexChanged(object sender, EventArgs e)
    {
        TblMenuEntity _TblMenuEntity = new TblMenuEntity();
        _TblMenuEntity.SortExpression = TblMenuFields.ThuTu.Name;
        _TblMenuEntity.SortDirection = "DESC";
        string txt = ddlDanhMucCha.SelectedItem.Text;
        txt = txt.Replace("--", "");
        _TblMenuEntity.Condition = txt;
        _TblMenuEntity.LangId = LangID;

        LoadDataToGrid(_TblMenuEntity);
    }
    private void LoadDataToGrid(TblMenuEntity _TblMenuEntity)
    {
        DataTable dt = _TblMenuManager.be_selectToGridView(_TblMenuEntity);
        if (dt != null)
        {
            lblTotalRecord.Text = dt.Rows.Count.ToString();
            dgvMenuList.DataSource = dt;
            dgvMenuList.DataBind();
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/AdminPortal/Pages/MenuAdd.aspx");
    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in dgvMenuList.Rows)
        {
            HtmlInputCheckBox chkID = (HtmlInputCheckBox)row.FindControl("chkID");
            if (chkID.Checked)
            {
                try
                {
                    _TblMenuManager.DeleteById(Convert.ToInt32(chkID.Value));
                }
                catch { }
            }
        }
        LoadDataToGrid(new TblMenuEntity());
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        TblMenuEntity _TblMenuEntity = new TblMenuEntity();
        _TblMenuEntity.SortExpression = TblMenuFields.ThuTu.Name;
        _TblMenuEntity.SortDirection = "ASC";
        _TblMenuEntity.Condition = txtKeySearch.Text;
        _TblMenuEntity.LangId = LangID;

        LoadDataToGrid(_TblMenuEntity);
    }
    protected void dgvMenuList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataRowView r = (DataRowView)e.Row.DataItem;

        HyperLink lnkMenu = (HyperLink)e.Row.FindControl("lnkMenu");

        Image imgUpdate = (Image)e.Row.FindControl("imgUpdate");

        if (r != null && lnkMenu != null && imgUpdate != null)
        {
            lnkMenu.Text = r[TblMenuFields.TieuDe.Name].ToString();
            lnkMenu.NavigateUrl = "~/AdminPortal/Pages/MenuEdit.aspx?id=" + r[TblMenuFields.Id.Name].ToString();

            if (r[TblMenuFields.TrangThai.Name].ToString().Equals("True"))
                imgUpdate.ImageUrl = "~/AdminPortal/Images/Icon/tree_dnd_yes.png";
            else
                imgUpdate.ImageUrl = "~/AdminPortal/Images/Icon/tree_dnd_no.png";
            imgUpdate.Attributes.Add("onclick", "javascript:changeStatusMenu('" + imgUpdate.ClientID + "','" + r[TblMenuFields.Id.Name].ToString() + "')");

        }
    }
}